package com.examples.ry.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class FillPoly {
	static final int points = 11;

	public static void main(String[] args) {
		final Point center = new Point(0, 0);
		final int[] radial = new int[points * 2];
		final Display display = new Display();
		final Color white = display.getSystemColor(SWT.COLOR_WHITE);
		final Shell shell = new Shell(display);
		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				Rectangle bounds = shell.getClientArea();
				center.x = bounds.x + bounds.width / 2;
				center.y = bounds.y + bounds.height / 2;
				int pos = 0;
				for (int i = 0; i < points; ++i) {
					double r = Math.PI * 2 * pos / points;
					radial[i * 2] = (int) ((1 + Math.cos(r)) * center.x);
					radial[i * 2 + 1] = (int) ((1 + Math.sin(r)) * center.y);
					pos = (pos + points / 2) % points;
				}
			}
		});
		shell.addListener(SWT.Paint, new Listener() {
			public void handleEvent(Event event) {
				event.gc.setBackground(white);
				event.gc.fillPolygon(radial);
				event.gc.drawPolygon(radial);
			}
		});
		shell.setText("Fill Polygon");
		shell.setSize(400, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
